/*
 * ComerciosReportesDetallesFrame.java
 *
 * Created on 7 de mayo de 2008, 03:05 PM
 */
package org.comercios.reportes;
 
import org.mybeans.Sesion;
import org.mybeans.ComerciosBeans;
 
import org.comercios.registros.ComerciosBusquedaFrame;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author  avbravo
 */
public class ComerciosReportesDetallesFrame extends javax.swing.JFrame {

    Sesion sesion;
    ComerciosBeans comerciosBeans;
    boolean focoComercio = false;

    /** Creates new form ComerciosReportesDetallesFrame */
    public ComerciosReportesDetallesFrame() {
        initComponents();
        sesion = new Sesion();
        comerciosBeans = new ComerciosBeans();
        /*
         * Fondo
         */

         
         
         

         
         
        /*
         * fondo
         */

        if (sesion.getIdComercio() == null || sesion.getIdComercio().equals("")) {
        } else {
            jTextFieldIdComercio.setText(sesion.getIdComercio());
            ValidarComercio();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldIdComercio = new javax.swing.JTextField();
        jTextFieldComercio = new javax.swing.JTextField();
        jButtonBuscar = new javax.swing.JButton();
        jButtonVistaPrevia = new javax.swing.JButton();
        jButtonLimpiar = new javax.swing.JButton();
        jButtonRegresar = new javax.swing.JButton();
        jButtonImprimir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Reporte de Detalles de Comercios");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Codigo");

        jLabel2.setText("Comercio");

        jTextFieldIdComercio.setColumns(35);
        jTextFieldIdComercio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldIdComercioActionPerformed(evt);
            }
        });

        jTextFieldComercio.setEditable(false);

        jButtonBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/search.png"))); // NOI18N
        jButtonBuscar.setToolTipText("Buscar");
        jButtonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarActionPerformed(evt);
            }
        });

        jButtonVistaPrevia.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/preview.png"))); // NOI18N
        jButtonVistaPrevia.setToolTipText("Vista Previa");
        jButtonVistaPrevia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVistaPreviaActionPerformed(evt);
            }
        });

        jButtonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/escoba.png"))); // NOI18N
        jButtonLimpiar.setToolTipText("Limpiar");
        jButtonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimpiarActionPerformed(evt);
            }
        });

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Regresar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jButtonImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/print.jpeg"))); // NOI18N
        jButtonImprimir.setToolTipText("Imprimir");
        jButtonImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonImprimirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(32, 32, 32)
                        .addComponent(jTextFieldIdComercio, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldComercio, javax.swing.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE)))
                .addGap(53, 53, 53))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jButtonVistaPrevia, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(282, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldIdComercio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldComercio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButtonImprimir, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
                    .addComponent(jButtonRegresar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonLimpiar, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                    .addComponent(jButtonVistaPrevia, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarActionPerformed

    ComerciosBusquedaFrame comerciosBusquedaFrame = new ComerciosBusquedaFrame();
    comerciosBusquedaFrame.setLocationRelativeTo(null);
    comerciosBusquedaFrame.setVisible(true);
    focoComercio = true;
}//GEN-LAST:event_jButtonBuscarActionPerformed

private void jButtonVistaPreviaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVistaPreviaActionPerformed


    String idComercio = this.jTextFieldIdComercio.getText();
    if (idComercio == null || idComercio.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese el codigo ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldIdComercio.requestFocus();
        return;
    }

    String comercio = this.jTextFieldComercio.getText();
    if (comercio == null || comercio.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese el comercio", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldComercio.requestFocus();
        return;
    }

    String reportSource;
    reportSource = sesion.getRutaReportes() + "ComerciosDetalles.jrxml";
    String reportDest;
    reportDest = sesion.getRutaDestino() + "ComerciosDetalles.html";

    Map<String, Object> params = new HashMap<String, Object>();

    try {
        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        JasperReport jasperReport =
                JasperCompileManager.compileReport(reportSource);
        params.put("P_IDMUNICIPIO", sesion.getIdmunicipio());
        params.put("P_IDCOMERCIO", idComercio);
        params.put("P_MUNICIPIO", sesion.getMunicipio());
        params.put("P_NOMBREUSUARIO", sesion.getUsuario());
        JasperPrint jasperPrint =
                JasperFillManager.fillReport(
                jasperReport, params, sesion.con);

        JasperViewer.viewReport(jasperPrint, false);

    } catch (JRException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonVistaPreviaActionPerformed

private void jButtonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimpiarActionPerformed

    Limpiar();
}//GEN-LAST:event_jButtonLimpiarActionPerformed

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jButtonImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImprimirActionPerformed


    String idComercio = this.jTextFieldIdComercio.getText();
    if (idComercio == null || idComercio.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese el codigo ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldIdComercio.requestFocus();
        return;
    }

    String comercio = this.jTextFieldComercio.getText();
    if (comercio == null || comercio.equals("")) {
        JOptionPane.showMessageDialog(this, "Ingrese el comercio", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jTextFieldComercio.requestFocus();
        return;
    }

    String reportSource;
    reportSource = sesion.getRutaReportes() + "ComerciosDetalles.jrxml";
    String reportDest;
    reportDest = sesion.getRutaDestino() + "ComerciosDetalles.html";

    Map<String, Object> params = new HashMap<String, Object>();

    try {
        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        JasperReport jasperReport =
                JasperCompileManager.compileReport(reportSource);
        params.put("P_IDMUNICIPIO", sesion.getIdmunicipio());
        params.put("P_IDCOMERCIO", idComercio);
        params.put("P_MUNICIPIO", sesion.getMunicipio());
        params.put("P_NOMBREUSUARIO", sesion.getUsuario());
        JasperPrint jasperPrint =
                JasperFillManager.fillReport(
                jasperReport, params, sesion.con);

        JasperPrintManager.printReport(jasperPrint, true);

    } catch (JRException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonImprimirActionPerformed

private void jTextFieldIdComercioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldIdComercioActionPerformed

    Consultar();
}//GEN-LAST:event_jTextFieldIdComercioActionPerformed

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
    // TODO add your handling code here:
    try {


        if (focoComercio) {
            ActualizarComercios();
        }

        focoComercio = false;
    } catch (Exception ex) {
        sesion.MostrarMensajeError("Error ", "Clase: PagosComerciosEventualesFrame.java\nBoton Actualizar Impuestos()\n " + ex.getMessage().toString());
    }
}//GEN-LAST:event_formWindowGainedFocus

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ComerciosReportesDetallesFrame().setVisible(true);
            }
        });
    }

    private void CargarComercios() {
        try {

            this.jTextFieldIdComercio.setText(comerciosBeans.getIdComercio());
            this.jTextFieldComercio.setText(comerciosBeans.getComercio());

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void Limpiar() {
        this.jTextFieldIdComercio.setText("");
        this.jTextFieldComercio.setText("");
    }

    private void Consultar() {
        try {
            String IdComercio = this.jTextFieldIdComercio.getText();
            if (IdComercio == null || IdComercio.equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese el codigo del comercio", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            String sqlquery = "select comercios.*, contribuyentes.contribuyente from comercios, contribuyentes where comercios.idmunicipio = ? and comercios.idcomercio = ? and comercios.cedula = contribuyentes.cedula";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            psquery.setString(1, sesion.getIdmunicipio());
            psquery.setString(2, IdComercio);
            ResultSet rs = psquery.executeQuery();
            rs.next();
            int x = rs.getRow();
            if (x == 0) {
                JOptionPane.showMessageDialog(this, "No existen registros de comercios con ese codigo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            String comercio = rs.getString("comercio");
            String cedula = rs.getString("cedula");
            String contribuyente = rs.getString("contribuyente");
            sesion.con.close();
            if (comercio == null || comercio.equals("")) {
                JOptionPane.showMessageDialog(this, "El comercio esta en blanco", "Mensaje", JOptionPane.WARNING_MESSAGE);
                return;
            }
            comerciosBeans.setIdComercio(IdComercio);
            comerciosBeans.setComercio(comercio);
            comerciosBeans.setCedula(cedula);
            comerciosBeans.setContribuyente(contribuyente);
            this.jTextFieldComercio.setText(comercio);

            repaint();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void ActualizarComercios() {
        try {
            CargarComercios();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void ValidarComercio() {
        try {

            String idComercio = this.jTextFieldIdComercio.getText();
            comerciosBeans.setIdComercio(idComercio);

            if (comerciosBeans.Consultar() == true) {
                CargarComercios();
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Metodo:ValidarComercio()\nError " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBuscar;
    private javax.swing.JButton jButtonImprimir;
    private javax.swing.JButton jButtonLimpiar;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JButton jButtonVistaPrevia;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFieldComercio;
    private javax.swing.JTextField jTextFieldIdComercio;
    // End of variables declaration//GEN-END:variables
}
